<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.username" placeholder="登录账号"></el-input>
				</el-form-item>

<!--				<el-form-item>-->
<!--				<el-select v-model="filters.parentId" clearable placeholder="上级部门">-->
<!--					<el-option-->
<!--							v-for="item in parentDepts"-->
<!--							:key="item.id"-->
<!--							:label="item.name"-->
<!--							:value="item.id">-->
<!--					</el-option>-->
<!--				</el-select>-->
<!--				</el-form-item>-->
<!--				<el-form-item>-->
<!--				<el-select v-model="filters.managerId" clearable placeholder="部门经理">-->
<!--					<el-option-->
<!--							v-for="item in manageremps"-->
<!--							:key="item.id"-->
<!--							:label="item.username"-->
<!--							:value="item.id">-->
<!--					</el-option>-->
<!--				</el-select>-->
<!--				</el-form-item>-->

				<el-form-item>
					<el-button type="primary" v-on:click="getdept">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
<!--		:data :绑定的数据-->
<!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
<!--		prop:对应的是:data绑定的对象的属性-->
<!--		label :描述-->
		<el-table :data="dept" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="登录账号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="100"  sortable>

			</el-table-column>
			<el-table-column prop="phone" label="手机号" width="120" sortable>
			</el-table-column>

			<el-table-column prop="age" label="年龄" width="100" sortable>
			</el-table-column>

			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
<!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="操作" :visible.sync="editFormVisible"  :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="登录账号" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="登录密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
<!--				<el-form-item label="部门状态" prop="state">-->
<!--				<el-switch-->
<!--						style="display: block"-->
<!--						v-model="state"-->
<!--						active-color="#13ce66"-->
<!--						inactive-color="#ff4949"-->
<!--						active-text="启用"-->
<!--						inactive-text="禁用">-->
<!--				</el-switch>-->
<!--				</el-form-item>-->

<!--				<el-form-item label="部门经理" prop="state">-->
<!--				<el-select v-model="editForm.managerId" clearable placeholder="部门经理">-->
<!--					<el-option-->
<!--							v-for="item in managerS"-->
<!--							:key="item.id"-->
<!--							:label="item.username"-->
<!--							:value="item.id">-->
<!--					</el-option>-->
<!--				</el-select>-->
<!--				</el-form-item>-->


<!--				<el-form-item label="上级部门">-->
<!--				<el-cascader-->
<!--						v-model="editForm.parentIds"-->
<!--						:options="treeList"-->
<!--						:props="{ checkStrictly: true ,value:'id',label:'name'}"-->
<!--						clearable>-->
<!--				</el-cascader>-->
<!--				</el-form-item>-->

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>

	//import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	//import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					username: '',
					parentId:null,
					managerId:null,
				},

				parentDepts:[],
				manageremps:[],
				managerS:[],
				treeList:[],
				dept: [],
				total: 0,
				page: 1,
				size:5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				state:true,
				//编辑界面数据
				editForm: {
					id:'',
					username: '',
					email: '',
					phone:'',
					age:'',
				},

			}
		},
		methods: {
			//性别显示转换
			//row 当前行row = user  column当前列
			formatSex: function (row, column) {
				return row.state == 1 ? '正常' : row.state == 0 ? '禁用' : '未知';
			},
			handleCurrentChange(val) {
				//var 就是当前页的数据
				this.page = val;
				this.getdept();
			},
			//获取部门列表
			getdept(flag) {
				//封装的数据
				let para = {
					//page 就是当前页
					page: this.page,
					size: this.size,
					//这个是高级查询的条件
					keyword: this.filters.username,
					parentId: this.filters.parentId,
					managerId: this.filters.managerId,
				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/emp/findPage",para).then((res) => {
					// console.log("后端发送过来的分页数据:"+JSON.stringify(res));
					//
					console.log(res)
					this.total = res.data.total;
					//
					this.dept = res.data.list;
                    //跳转页面
					if (this.total % 5 == 0&&flag!=2){
						this.page = this.page-1
						this.getdept(2)
					}
					//关闭进度条
					this.listLoading = false;

					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/emp/"+row.id).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getdept();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				//显示编辑页面的模态框
				this.editFormVisible = true;
				//Object.assign 是拷贝对象 BeanUtil.copyof()
				this.editForm = row;
				//显示正常状态
				this.state = !! row.state
				let dirPath=row.dirPath;
				console.log(dirPath)
				let paths=dirPath.split("/");
				paths=paths.slice(1,paths.length-1);
				paths=paths.map(Number)
				this.editForm.parentIds=paths
				console.log("当前修改的数据是:"+JSON.stringify(this.editForm))
			},
			//新增显示
			handleAdd: function () {
				//显示编辑页面的模态框
				this.editFormVisible = true;
				//清空模态框
				this.state=true
				this.editForm={
					name:'',
					sn:'',
				}

			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							//这里面是获取数据的
							this.editLoading = true;
							//状态
							this.editForm.state = this.state ? 1:0
							//拷贝对象
							let para = Object.assign({}, this.editForm);
								this.$http.post("/emp",para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: res.data.success?'提交成功':res.data.message,
									type: res.data.success?'success':'error'
									// message: '提交成功',
									// type: 'success'
								});
								this.$refs['editForm'].resetFields();
								//关闭模态框
								this.editFormVisible = false;
								this.getdept();
							});
						});
					}
				});
			},

			//复选勾选框的方法
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = {
						'ids': ids,


					};
					this.$http.post("/emp/deleteIds/"+ids).then((res) => {
						console.log(res)
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getdept();
					});
				}).catch(() => {

				});
			},
			parentDept() {
              this.$http.post("/dept/parentDept").then(res=>{
				  console.log("1")
				  console.log(res)
				  this.parentDepts = res.data.data
			  })
	      },
			managers() {
              this.$http.get("/emp/managerAll").then(res=>{
				  console.log("2")
				  console.log(res)
				  this.manageremps = res.data.data
			  })
	      },
			managerSI() {
              this.$http.get("/emp").then(res=>{
				  console.log(res)
				  this.managerS = res.data.data
			  })
	      },
			treeLists() {
              this.$http.get("/dept/treeList").then(res=>{
				  console.log(res)
				  this.treeList = res.data.data
			  })
	      },
		},
		//就是页面已加载完成就会去触发这个mounted
		mounted() {

			this.getdept();
			查询上级部门
			this.parentDept();
            //查询经理们
			this.managers();
			//新增经理们
			this.managerSI();
			//级联选择器
			this.treeLists();
		}
	}

</script>

<style scoped>

</style>
